<?php

include "header.php";

header('Content-Type: text/html;charset=utf-8');

function func_chgtitle($sourcestr,$cutlength)
{
    $returnstr='';
    $i=0;
    $n=0;
    $str_length=strlen($sourcestr);//字符串的字节数
    while (($n<$cutlength) and ($i<=$str_length))
    {
        $temp_str=substr($sourcestr,$i,1);
        $ascnum=Ord($temp_str);//得到字符串中第$i位字符的ascii码
        if ($ascnum>=224) //如果ASCII位高与224，
        {
            $returnstr=$returnstr.substr($sourcestr,$i,3);
    //根据UTF-8编码规范，将3个连续的字符计为单个字符
            $i=$i+3; //实际Byte计为3
            $n++; //字串长度计1
        }
        elseif ($ascnum>=192) //如果ASCII位高与192，
        {
            $returnstr=$returnstr.substr($sourcestr,$i,2);
            //根据UTF-8编码规范，将2个连续的字符计为单个字符
            $i=$i+2; //实际Byte计为2
            $n++; //字串长度计1
        }
        elseif ($ascnum>=65 && $ascnum<=90)
    //如果是大写字母，
        {
            $returnstr=$returnstr.substr($sourcestr,$i,1);
            $i=$i+1; //实际的Byte数仍计1个
            $n++; //但考虑整体美观，大写字母计成一个高位字符
        }
        else //其他情况下，包括小写字母和半角标点符号，
        {
            $returnstr=$returnstr.substr($sourcestr,$i,1);
            $i=$i+1; //实际的Byte数计1个
            $n=$n+0.5; //小写字母和半角标点等与半个高位字符宽…
        }
    }
    if ($str_length>$cutlength){
        $returnstr = $returnstr;
    }
    return $returnstr;
}

$m = new m;
$_GET['act'] = $_GET['act'] ? $_GET['act'] : "index";
$m->$_GET['act']();
class m
{
    private $url_arr = array(
        's1.izhushou.cn',
        's2.izhushou.cn',
        's3.izhushou.cn');

    private function get_dsp(){
        $conn_mysql = _mysqllogon('Mysql_102');

        $sql_d = "select DSP_ID from ios_apps_dsp where file_id=10000 order by dsp_id";
        $stmt_d = _mysqlparse($conn_mysql, $sql_d);
        _mysqlexecute($stmt_d);
        $dsp_id = array();
        while ($d_row = mysql_fetch_assoc($stmt_d))
        {
            $dsp_id[] = $d_row['DSP_ID'];
        }
        return $dsp_id;
    }

    private  function get_apps($dsp_ids,$genre_id,$num,$pageID=NULL,$order_by,$t_len,$type=1,$words=NULL){
        $dsp_arr = implode(',',$dsp_ids);
        $conn_mysql = _mysqllogon('Mysql_102');

        $memcache_server = new memcache_server('102');
        $ipad_app_memcache_time = $memcache_server->get("ipad_app_memcache_time");
        if(!$ipad_app_memcache_time){
            $ipad_app_memcache_time = date("Y-m-d H:i:s");
            $memcache_server->set("ipad_app_memcache_time",$ipad_app_memcache_time);
        }
        $memcache_server->close();
        if($pageID){
            $sql_t = "select count(*) as TOTAL from ios_apps_info i
              where i.status=1 and i.ipad_no=i.id
              and i.plist_url is not null and i.plist_url !=''
              and i.dsp_id in (".$dsp_arr.")
              and i.add_time<=:update_time";
            if($genre_id){
                if($genre_id>7000){
                    $sql_t .= " and i.sub_genre = :genre_id";
                }else{
                    if($type==1){
                        $sql_t .= " and i.genre_id = :genre_id";
                    }else{
                        $sql_t .= " and i.genre_id != :genre_id";
                    }
                }
            }
            if($words){
                $sql_t .= " and i.app_title like :words";
            }
            $stmt_t = _mysqlparse($conn_mysql, $sql_t);
            _mysqlbindbyname($stmt_t, ":update_time", $ipad_app_memcache_time);
            if($genre_id){
                _mysqlbindbyname($stmt_t, ":genre_id", $genre_id);
            }
            if($words){
                _mysqlbindbyname($stmt_t, ":words", trim("%{$words}%"));
            }
            $mysqlerror_t = _mysqlexecute($stmt_t);
            if ($mysqlerror_t) {
                die(json_encode(array(
                    'error' => 5002,
                    'message' => 'system error,select total failed',
                    'sql' => $sql_t.'|||'.$words
                )));
            }
            $t_row = mysql_fetch_assoc($stmt_t);
            $total = intval($t_row['TOTAL']);

            $this->pageObj = new page($total, $num);
            if($pageID > $this->pageObj->pages){
                $pageID = $_GET['pageID'] = $this->pageObj->pages;
                $this->pageObj = new page($total, $num);
            }
        }

        $sql_a = "select ID,APP_TITLE,ICON,APP_SIZE,";
        if($order_by == 'download_zhou desc'){
            $sql_a .= "DOWNLOAD_ZHOU AS DOWNLOAD,";
        }else if($order_by == 'download_yue desc'){
            $sql_a .= "DOWNLOAD_YUE AS DOWNLOAD,";
        }else{
            $sql_a .= "DOWNLOAD,";
        }
        $sql_a .= "IMGS,PLIST_URL,INTRODUCTION
              from ios_apps_info i
              where i.status=1 and i.ipad_no=i.id
            	and i.plist_url is not null and i.plist_url !=''
              and i.dsp_id in (".$dsp_arr.")
              and i.add_time<=:update_time";
        if($genre_id){
            if($genre_id>7000){
                $sql_a .= " and i.sub_genre = :genre_id";
            }else{
                if($type==1){
                    $sql_a .= " and i.genre_id = :genre_id";
                }else{
                    $sql_a .= " and i.genre_id != :genre_id";
                }
            }
        }
        if($words){
            $sql_a .= " and i.app_title like :words";
        }

        if($order_by == 'order_by_re asc'){
            $sql_a .= " and i.recommend = 1";
        }

        if($pageID){
            $sql_a .= " order by i.".$order_by.",i.id asc LIMIT " . $this->pageObj->limit_1 . "," . $this->pageObj->limit_2;
        }else{
            $sql_a .= " order by i.".$order_by.",i.id asc limit 0," . $num;
        }
        $stmt_a = _mysqlparse($conn_mysql, $sql_a);
        _mysqlbindbyname($stmt_a, ":update_time", $ipad_app_memcache_time);
        if($genre_id){
            _mysqlbindbyname($stmt_a, ":genre_id", $genre_id);
        }
        if($words){
            _mysqlbindbyname($stmt_a, ":words", trim("%{$words}%"));
        }
        $mysql_err = _mysqlexecute($stmt_a);
        if($mysql_err){
            print_r($mysql_err);
        }
        $app_arr = array();
        while ($a_row = mysql_fetch_assoc($stmt_a))
        {
            $icon_arr = explode('|', $a_row['ICON']);
            $pic_arr = unserialize($a_row['IMGS']);
            $a_row['ICON'] = str_replace('iosfile.izhushou.cn',$this->url_arr[array_rand($this->url_arr,1)],$icon_arr[0]);
            if(!strstr($pic_arr['iPad'][0],'http')){
                $a_row['IMG'] = 'http://'.$this->url_arr[array_rand($this->url_arr,1)].$pic_arr['iPad'][0];
            }else{
                $a_row['IMG'] = $pic_arr['iPad'][0];
            }
            if($t_len){
                $a_row['APP_TIT'] = func_chgtitle($a_row['APP_TITLE'],$t_len);
            }else{
                $a_row['APP_TIT'] = $a_row['APP_TITLE'];
            }
            $app_arr[] = $a_row;
        }
        $return_array['total'] = $total;
        $return_array['page'] = $pageID;
        $return_array['pages'] = $this->pageObj->pages;
        $return_array['list'] = $app_arr;

        if($pageID){
            return $return_array;
        }else{
            return $app_arr;
        }
    }

    function index(){
        $dsp_ids = $this->get_dsp();
        $genre_name_game = array(
            '7001'=>'动作游戏',
            '7002'=>'探险游戏',
            '7003'=>'街机游戏',
            '7009'=>'家庭游戏'
        );
        $genre_name = array(
            '6008'=>'摄影录像',
            '6023'=>'美食佳饮',
            '6013'=>'健康健美',
            '6022'=>'商品指南'
        );
        $genre_arr_game = $genre_arr = array();

        $memcache_server = new memcache_server('102');
        $genre_arr_game = $memcache_server->get("ipad_app_genre_game");
        $genre_arr = $memcache_server->get("ipad_app_genre_app");

        $app_hot_arr = $memcache_server->get("ipad_app_hot");
        $app_new_game = $memcache_server->get("ipad_app_new_game");
        $app_new_app = $memcache_server->get("ipad_app_new_app");
        $app_top_game = $memcache_server->get("ipad_app_top_game");
        $app_top_app = $memcache_server->get("ipad_app_top_app");

        if(empty($genre_arr_game)){
            foreach ($genre_name_game as $k=>$v)
            {
                $genre_arr_game[$k] = $this->get_apps($dsp_ids,$k,5,NULL,'download desc',9);
            }
            $memcache_server->set("ipad_app_genre_game", $genre_arr_game);
        }

        if(empty($genre_arr)){
            foreach ($genre_name as $k=>$v)
            {
                $genre_arr[$k] = $this->get_apps($dsp_ids,$k,5,NULL,'download desc',9);
            }
            $memcache_server->set("ipad_app_genre_app", $genre_arr);
        }

        if(empty($app_hot_arr)){
            $app_hot_arr = $this->get_apps($dsp_ids,NULL,18,NULL,'download desc',6);
            $memcache_server->set("ipad_app_hot", $app_hot_arr);
        }
        if(empty($app_new_game)){
            $app_new_game = $this->get_apps($dsp_ids,6014,6,NULL,'update_time desc',NULL);
            $memcache_server->set("ipad_app_new_game", $app_new_game);
        }
        if(empty($app_new_app)){
            $app_new_app = $this->get_apps($dsp_ids,6014,6,NULL,'update_time desc',NULL,2);
            $memcache_server->set("ipad_app_new_app", $app_new_app);
        }
        if(empty($app_top_game)){
            $app_top_game = $this->get_apps($dsp_ids,6014,10,NULL,'download desc',NULL);
            $memcache_server->set("ipad_app_top_game", $app_top_game);
        }
        if(empty($app_top_app)){
            $app_top_app = $this->get_apps($dsp_ids,6014,10,NULL,'download desc',NULL,2);
            $memcache_server->set("ipad_app_top_app", $app_top_app);
        }
        $memcache_server->close();

        include "html/ipad/index.html";
    }

    function app_list(){
        $_GET['type'] = $_GET['type']=='app' ? 'app' : 'game';
        $_GET['order'] = $_GET['order']=='recommend' ? 'recommend' : 'new';
        $_GET['viewType'] = $_GET['viewType']=='simple' ? 'simple' : 'picture';
        $_GET['categoryId'] = $_GET['categoryId'] ? $_GET['categoryId'] : NULL;
        $_GET['pageID'] = isset($_GET['pageID']) ? max(intval($_GET['pageID']), 1) : 1;
        $_GET['ajax'] = $_GET['ajax'] ? $_GET['ajax'] : NULL;

        $dsp_ids = $this->get_dsp();
        $genre_name_id = array(
            '7011','7016','7014','7005','7006'
        );

        $genre_name = array(
            '7011'=>'音乐游戏',
            '7016'=>'体育游戏',
            '7014'=>'角色扮演',
            '7005'=>'扑克牌',
            '7006'=>'娱乐场'
        );

        $order_by = $_GET['order']=='recommend' ? 'download desc' : 'update_time desc';
        $memcache_server = new memcache_server('102');
        $genre_arr = $memcache_server->get("ipad_app_category_" . $_GET['type']);

        $cate = $_GET['categoryId'] ? $_GET['categoryId'] : $_GET['type'];
        $cate_id = $_GET['categoryId'] ? $_GET['categoryId'] : 6014;
        $cate_type = 1;
        if(!$_GET['categoryId']){
            $cate_type = $_GET['type']=='app' ? 2 : 1;
        }

        if($_GET['pageID']<=3){
            $app_list_arr = $memcache_server->get("ipad_app_list_" . $_GET['order'] . "_" . $cate ."_".$_GET['pageID']);
            if(empty($app_list_arr)){
                $app_list_arr = $this->get_apps($dsp_ids,$cate_id,48,$_GET['pageID'],$order_by,NULL,$cate_type);
                $memcache_server->set("ipad_app_list_" . $_GET['order'] . "_" . $cate ."_".$_GET['pageID'], $app_list_arr);
            }
        }else{
            $app_list_arr = $this->get_apps($dsp_ids,$cate_id,48,$_GET['pageID'],$order_by,NULL,$cate_type);
        }

        if($_GET['viewType'] == 'picture'){
            if($_GET['ajax'] == 1){
                $_GET['scrollPage'] = (intval($_GET['scrollPage'])>= 2||intval($_GET['scrollPage'])<=4) ? intval($_GET['scrollPage']) : 2;
                $app_list_page['Items'] = array_slice($app_list_arr['list'],($_GET['scrollPage']*12),12);
                echo json_encode($app_list_page);
                exit;
            }
            $app_list_arr['list'] = array_slice($app_list_arr['list'],0,24);
        }
        $app_list_arr_re = $this->get_apps($dsp_ids,$cate_id,5,NULL,'order_by_re asc',NULL,$cate_type);

        if($app_list_arr['pages']<6){
            $page_arr = array();
            for($i=1;$i<=$app_list_arr['pages'];$i++){
                $page_arr[] = $i;
            }
        }else{
            if($_GET['pageID']<=3){
                $page_arr = array(1,2,3,4,5,6);
            }else if($_GET['pageID'] >= $app_list_arr['pages'] - 3){
                $page_arr = array($app_list_arr['pages']-5,$app_list_arr['pages']-4,$app_list_arr['pages']-3,$app_list_arr['pages']-2,$app_list_arr['pages']-1,$app_list_arr['pages']);
            }else{
                $page_arr = array($_GET['pageID']-2,$_GET['pageID']-1,$_GET['pageID'],$_GET['pageID']+1,$_GET['pageID']+2,$_GET['pageID']+3);
            }
        }

        if(empty($genre_arr)){
            $conn_mysql = _mysqllogon('Mysql_102');

            $where_a = '';
            if ($_GET['type'] == 'game') {
                $where_a .= ' where parent_id = 6014';
            } else if ($_GET['type'] == 'app') {
                $where_a .= ' where parent_id != 6014 and id != 6014 and parent_id = 0';
            }

            $sql = "select ID,GENRE from ios_apps_genre
                  ".$where_a." order by order_by ";
            $stmt = _mysqlparse($conn_mysql, $sql);
            _mysqlexecute($stmt);
            $genre_arr = array();
            while ($_row = mysql_fetch_assoc($stmt))
            {
                if(in_array($_row['ID'],$genre_name_id)){
                    $_row['GENRE'] = $genre_name[$_row['ID']];
                }
                if($_row['ID'] == 6008){
                    $_row['GENRE'] = '摄影录像';
                }
                $genre_arr[] = $_row;
            }
            $memcache_server->set("ipad_app_category_" . $_GET['type'], $genre_arr);
        }
        $memcache_server->close();
        if($_GET['type'] == 'game'){
            $title_type = '游戏';
            $cls = 'ipa_gam_imgs';
            $cls1 = 'app_gam_sub';
            $cls2 = 'app_gam_sup';
            $t_type = 'game';
        }else{
            $title_type = '应用';
            $cls = 'ipa_soft_imgs';
            $cls1 = 'app_list_sub';
            $cls2 = 'soft_sup';
            $t_type = 'soft';
        }
        include "html/ipad/list_app.html";
    }

    function rank(){
        ini_set("display_errors",true);
        $order_arr = array('week','month','total');
        $_GET['type'] = $_GET['type']=='app' ? 'app' : 'game';
        $_GET['order'] = in_array($_GET['order'],$order_arr) ? $_GET['order'] : 'total';
        $_GET['viewType'] = $_GET['viewType']=='simple' ? 'simple' : 'picture';
        $_GET['categoryId'] = $_GET['categoryId'] ? $_GET['categoryId'] : NULL;
        $_GET['pageID'] = isset($_GET['pageID']) ? max(intval($_GET['pageID']), 1) : 1;
        $_GET['ajax'] = $_GET['ajax'] ? $_GET['ajax'] : NULL;

        $dsp_ids = $this->get_dsp();
        $genre_name_id = array(
            '7011','7016','7014','7005','7006'
        );

        $genre_name = array(
            '7011'=>'音乐游戏',
            '7016'=>'体育游戏',
            '7014'=>'角色扮演',
            '7005'=>'扑克牌',
            '7006'=>'娱乐场'
        );

        $order_by_arr = array(
            'week'  => 'download_zhou desc',
            'month' => 'download_yue desc',
            'total' => 'download desc');

        $order_by = $order_by_arr[$_GET['order']];
        $memcache_server = new memcache_server('102');
        $genre_arr = $memcache_server->get("ipad_app_category_" . $_GET['type']);

        $cate = $_GET['categoryId'] ? $_GET['categoryId'] : $_GET['type'];
        $cate_id = $_GET['categoryId'] ? $_GET['categoryId'] : 6014;
        $cate_type = 1;
        if(!$_GET['categoryId']){
            $cate_type = $_GET['type']=='app' ? 2 : 1;
        }

        if($_GET['pageID']<=3){
            $app_list_arr = $memcache_server->get("ipad_app_rank_" . $_GET['order'] . "_" . $cate ."_".$_GET['pageID']);
            if(empty($app_list_arr)){
                $app_list_arr = $this->get_apps($dsp_ids,$cate_id,48,$_GET['pageID'],$order_by,NULL,$cate_type);
                $memcache_server->set("ipad_app_rank_" . $_GET['order'] . "_" . $cate ."_".$_GET['pageID'], $app_list_arr);
            }
        }else{
            $app_list_arr = $this->get_apps($dsp_ids,$cate_id,48,$_GET['pageID'],$order_by,NULL,$cate_type);
        }

        if($_GET['viewType'] == 'picture'){
            if($_GET['ajax'] == 1){
                $_GET['scrollPage'] = (intval($_GET['scrollPage'])>= 2||intval($_GET['scrollPage'])<=4) ? intval($_GET['scrollPage']) : 2;
                $app_list_page['Items'] = array_slice($app_list_arr['list'],($_GET['scrollPage']*12),12);
                foreach($app_list_page['Items'] as $k=>$v){
                    $app_list_page['Items'][$k]['rank'] = ($_GET['pageID']-1)*48 + ($_GET['scrollPage']*12)+$k+1;
                }
                echo json_encode($app_list_page);
                exit;
            }
            $app_list_arr['list'] = array_slice($app_list_arr['list'],0,24);
        }
        $app_list_arr_re = $this->get_apps($dsp_ids,$cate_id,5,NULL,'order_by_re asc',NULL,$cate_type);

        if($app_list_arr['pages']<6){
            $page_arr = array();
            for($i=1;$i<=$app_list_arr['pages'];$i++){
                $page_arr[] = $i;
            }
        }else{
            if($_GET['pageID']<=3){
                $page_arr = array(1,2,3,4,5,6);
            }else if($_GET['pageID'] >= $app_list_arr['pages'] - 3){
                $page_arr = array($app_list_arr['pages']-5,$app_list_arr['pages']-4,$app_list_arr['pages']-3,$app_list_arr['pages']-2,$app_list_arr['pages']-1,$app_list_arr['pages']);
            }else{
                $page_arr = array($_GET['pageID']-2,$_GET['pageID']-1,$_GET['pageID'],$_GET['pageID']+1,$_GET['pageID']+2,$_GET['pageID']+3);
            }
        }

        if(empty($genre_arr)){
            $conn_mysql = _mysqllogon('Mysql_102');

            $where_a = '';
            if ($_GET['type'] == 'game') {
                $where_a .= ' where parent_id = 6014';
            } else if ($_GET['type'] == 'app') {
                $where_a .= ' where parent_id != 6014 and id != 6014 and parent_id = 0';
            }

            $sql = "select ID,GENRE from ios_apps_genre
                  ".$where_a." order by order_by ";
            $stmt = _mysqlparse($conn_mysql, $sql);
            _mysqlexecute($stmt);
            $genre_arr = array();
            while ($_row = mysql_fetch_assoc($stmt))
            {
                if(in_array($_row['ID'],$genre_name_id)){
                    $_row['GENRE'] = $genre_name[$_row['ID']];
                }
                if($_row['ID'] == 6008){
                    $_row['GENRE'] = '摄影录像';
                }
                $genre_arr[] = $_row;
            }
            $memcache_server->set("ipad_app_category_" . $_GET['type'], $genre_arr);
        }
        $memcache_server->close();
        if($_GET['type'] == 'game'){
            $title_type = '游戏';
            $cls = 'ipa_bk_game_imgs';
            $cls1 = 'app_gam_sub';
            $cls2 = 'app_gam_sup';
            $t_type = 'game';
        }else{
            $title_type = '应用';
            $cls = 'ipa_bk_soft_imgs';
            $cls1 = 'app_list_sub';
            $cls2 = 'soft_sup';
            $t_type = 'soft';
        }
        include "html/ipad/rank.html";
    }

    function search(){
        $_GET['words'] = $_GET['words'] ? $_GET['words'] : NULL;
        $_GET['pageID'] = isset($_GET['pageID']) ? max(intval($_GET['pageID']), 1) : 1;

        if($_GET['words']){
            $dsp_ids = $this->get_dsp();
            $app_list = $this->get_apps($dsp_ids,NULL,40,$_GET['pageID'],'update_time desc',NULL,1,$_GET['words']);

            if($app_list['pages']<6){
                $page_arr = array();
                for($i=1;$i<=$app_list['pages'];$i++){
                    $page_arr[] = $i;
                }
            }else{
                if($_GET['pageID']<=3){
                    $page_arr = array(1,2,3,4,5,6);
                }else if($_GET['pageID'] >= $app_list_arr['pages'] - 3){
                    $page_arr = array($app_list['pages']-5,$app_list['pages']-4,$app_list['pages']-3,$app_list['pages']-2,$app_list['pages']-1,$app_list['pages']);
                }else{
                    $page_arr = array($_GET['pageID']-2,$_GET['pageID']-1,$_GET['pageID'],$_GET['pageID']+1,$_GET['pageID']+2,$_GET['pageID']+3);
                }
            }
        }

        include "html/ipad/search.html";
    }
    function app_info(){
        $_GET['id'] = isset($_GET['id']) ? intval($_GET['id']) : 0;

        $conn_mysql = _mysqllogon('Mysql_102');

        $sql = "select i.ID,i.APP_TITLE,i.BUNDLE_VERSION,i.MINIMUM_OS_VERSION,
                i.APP_SIZE,i.PIC,i.VERSION_ID,i.PLIST_URL,i.IMGS,i.CONTENT,
                i.IPAD_NO,i.IPHONE_NO,i.APP_PRICE,i.SUB_GENRE,i.GENRE_ID,
                case i.iphone_no when i.id then 1 else 0 end SUP_IPHONE,
                date_format(i.update_time,'%Y-%m-%d') as I_UPDATE_TIME,i.DOWNLOAD
                from ios_apps_info i
                 where i.status=1 and i.id=:id and i.ipad_no=i.id
            	and i.plist_url is not null and i.plist_url !='' limit 0,1";
        $stmt = _mysqlparse($conn_mysql, $sql);
        _mysqlbindbyname($stmt, ":id", $_GET['id']);
        _mysqlexecute($stmt);
        $_row = mysql_fetch_assoc($stmt);
        if($_row['ID'])
        {
            $genre_id = $_row['SUB_GENRE'] ? $_row['SUB_GENRE'] : $_row['GENRE_ID'];
            $sql_g = "select GENRE from ios_apps_genre where id = :id";
            $stmt_g = _mysqlparse($conn_mysql, $sql_g);
            _mysqlbindbyname($stmt_g, ':id', $genre_id);
            _mysqlexecute($stmt_g);
            $g_row = mysql_fetch_assoc($stmt_g);
            if ($g_row['GENRE']) {
                $_row['GENRE'] = $g_row['GENRE'];
            }
            $_row['GENRE_CAT'] = $genre_id;

            $icon_arr = explode('|', $_row['PIC']);
            $_row['PIC'] = str_replace('iosfile.izhushou.cn',$this->url_arr[array_rand($this->url_arr,1)],$icon_arr[0]);
            $_row['IMGS'] = unserialize($_row['IMGS']);

            $app_info = $_row;

            if($_row['GENRE_ID'] == 6014){
                $t_type = 'game';
            }else{
                $t_type = 'soft';
            }
        }
        include "html/ipad/app_info.html";
    }
}
?>